package com.sunchangming.rpcgen;

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Random;

import junit.framework.Assert;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.sunchangming.gs.CLogin;

public class CollMarshalTest {

	private static Random rand;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		rand = new Random();
	}

	private static final String characters = "abcdefghijklmnopqrstuvwxzy0123456789;";

	private static String getRandString() {
		final int length = 64;
		final char[] text = new char[length];
		for (int i = 0; i < length; i++) {
			text[i] = characters.charAt(rand.nextInt(characters.length()));
		}
		return new String(text);

	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void test1() throws Exception {
		byte[] ret;
		int[] testInt={Integer.MIN_VALUE,0,1,1<<7,(1<<7)+8,1<<14,(1<<14)+8,1<<21,(1<<21)+8,1<<28,(1<<28)+8,Integer.MAX_VALUE};
		for(int i:testInt){
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			try {
				DataOutputStream dos = new DataOutputStream(baos);
				try {
					com.tianshen.message.MessageDecoder.writeRawVarint32(dos, i);
					dos.flush();
					baos.flush();
					ret = baos.toByteArray();
				} finally {
					dos.close();
				}
			} finally {
				baos.close();
			}
			ByteArrayInputStream bais = new ByteArrayInputStream(ret);

			DataInputStream dis = new DataInputStream(bais);
			int r=com.tianshen.message.MessageDecoder.readRawVarint32(dis);
			Assert.assertEquals(i, r);
			bais.close();
			dis.close();
		}
	}
	@Test
	public void test2() throws Exception {
		final com.sunchangming.test.TestColl v1 = new com.sunchangming.test.TestColl();
		int length;
		length = rand.nextInt(1024);
		for (int i = 0; i != length; ++i) {
			v1.getT1().add(getRandString());
		}
		length = rand.nextInt(1024);
		for (int i = 0; i != length; ++i) {
			v1.getT2().add(rand.nextInt());
		}
		length = rand.nextInt(1024);
		for (int i = 0; i != length; ++i) {
			final CLogin c = new CLogin();
			c.setAuth(getRandString());
			c.setSign(getRandString());
			v1.getT3().add(c);
		}
		length = rand.nextInt(1024);
		for (int i = 0; i != length; ++i) {
			v1.getT11().put(rand.nextInt(), rand.nextInt());
		}

		byte[] ret;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			DataOutputStream dos = new DataOutputStream(baos);
			try {
				v1.marshal(dos);
				dos.flush();
				baos.flush();
				ret = baos.toByteArray();
			} finally {
				dos.close();
			}
		} finally {
			baos.close();
		}
		final com.sunchangming.test.TestColl v2 = new com.sunchangming.test.TestColl();

		ByteArrayInputStream bais = new ByteArrayInputStream(ret);

		DataInputStream dis = new DataInputStream(bais);
		v2.unmarshal(dis);
		bais.close();
		dis.close();

		assertEquals(v1.getT1(), v2.getT1());
		assertEquals(v1.getT2(), v2.getT2());
		assertEquals(v1.getT3(), v2.getT3());
		assertEquals(v1.getT4(), v2.getT4());
		assertEquals(v1.getT5(), v2.getT5());
		assertEquals(v1.getT6(), v2.getT6());
		assertEquals(v1.getT7(), v2.getT7());
		assertEquals(v1.getT8(), v2.getT8());
		assertEquals(v1.getT9(), v2.getT9());
		assertEquals(v1.getT12(), v2.getT12());
		assertEquals(v1.getT13(), v2.getT13());
		assertEquals(v1.getT11(), v2.getT11());

		assertEquals(v1.getT1().hashCode(), v2.getT1().hashCode());
		assertEquals(v1.getT2().hashCode(), v2.getT2().hashCode());
		assertEquals(v1.getT3().hashCode(), v2.getT3().hashCode());
		assertEquals(v1.getT4().hashCode(), v2.getT4().hashCode());
		assertEquals(v1.getT5().hashCode(), v2.getT5().hashCode());
		assertEquals(v1.getT6().hashCode(), v2.getT6().hashCode());
		assertEquals(v1.getT7().hashCode(), v2.getT7().hashCode());
		assertEquals(v1.getT8().hashCode(), v2.getT8().hashCode());
		assertEquals(v1.getT9().hashCode(), v2.getT9().hashCode());
		assertEquals(v1.getT12().hashCode(), v2.getT12().hashCode());
		assertEquals(v1.getT13().hashCode(), v2.getT13().hashCode());
		assertEquals(v1.getT11().hashCode(), v2.getT11().hashCode());

		// 手动比较。
		for (int i = 0; i != v1.getT3().size(); ++i) {
			assertEquals(v1.getT3().get(i).getAuth(), v2.getT3().get(i)
					.getAuth());
			assertEquals(v1.getT3().get(i).getSign(), v2.getT3().get(i)
					.getSign());
		}
	}
}
